Micron Document
baubs git

Node / mirrors / respira / files / src / hooks / utility / usePrevious.test.ts

Displaying Raw • Download

src/hooks/utility/usePrevious.test.ts copilot/create-shared-infocard-component (7c3f79ae) Text, 1.49 KB

import { describe, it, expect } from "vitest";
import { renderHook } from "@testing-library/react";
import { usePrevious } from "./usePrevious";

describe("usePrevious", () => {
it("should return undefined on initial render", () => {
const { result } = renderHook(() => usePrevious(5));
expect(result.current).toBeUndefined();
});

it("should return previous value after update", () => {
const { result, rerender } = renderHook(({ value }) => usePrevious(value), {
initialProps: { value: 5 },
});

expect(result.current).toBeUndefined();

rerender({ value: 10 });
expect(result.current).toBe(5);

rerender({ value: 15 });
expect(result.current).toBe(10);
});

it("should handle different types of values", () => {
const { result, rerender } = renderHook(({ value }) => usePrevious(value), {
initialProps: { value: "hello" as string | number | null },
});

expect(result.current).toBeUndefined();

rerender({ value: 42 });
expect(result.current).toBe("hello");

rerender({ value: null });
expect(result.current).toBe(42);
});

it("should handle object references", () => {
const obj1 = { name: "first" };
const obj2 = { name: "second" };

const { result, rerender } = renderHook(({ value }) => usePrevious(value), {
initialProps: { value: obj1 },
});

expect(result.current).toBeUndefined();

rerender({ value: obj2 });
expect(result.current).toBe(obj1);
});
});

Served by rngit 1.3.3 - Generated in 0.06s